package com.adopapa.nezhacloud.authc.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.adopapa.nezhacloud.authc.domain.ResourceInfo;
import com.adopapa.nezhacloud.authc.domain.ResourceInfoRepository;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.service.BaseDomainService;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;


@Service
public class ResourceInfoService extends BaseDomainService<ResourceInfo> {

	@Autowired
	private ResourceInfoRepository resourceInfoRepository;

	public ResourceInfo getResourceInfoById(String id) {
		Optional<ResourceInfo> optional = resourceInfoRepository.findById(id);
		return optional.isPresent() ? optional.get() : null;
	}
	

	public Page<ResourceInfo> getResourceInfoPage(int pageNo, int pageSize, ResourceInfo resourceInfo) {
		PageRequest pageRequest = buildPageRequest(pageNo, pageSize, Order.desc("resourceUrl"));
		Specification<ResourceInfo> specification = buildSpecification(resourceInfo);
		Page<ResourceInfo> page = resourceInfoRepository.findAll(specification, pageRequest);
		return page;
	}

	public ResourceInfo saveResourceInfo(ResourceInfo resourceInfo) {
		return resourceInfoRepository.saveAndFlush(resourceInfo);
	}

	public List<ResourceInfo> saveResourceInfos(List<ResourceInfo> resourceInfos) {
		return resourceInfoRepository.saveAllAndFlush(resourceInfos);
	}

	public void deleteResourceInfo(String id) {
		resourceInfoRepository.deleteById(id);
	}

	public void deleteResourceInfo(ResourceInfo resourceInfo) {
		resourceInfoRepository.delete(resourceInfo);
	}

	public void deleteResourceInfos(String... ids) {
		resourceInfoRepository.deleteAllByIdInBatch(Arrays.asList(ids));
	}

	public void deleteResourceInfos(List<ResourceInfo> resourceInfos) {
		resourceInfoRepository.deleteAllInBatch(resourceInfos);
	}

	@SuppressWarnings("serial")
	@Override
	public Specification<ResourceInfo> buildSpecification(ResourceInfo resourceInfo) {
		return new Specification<ResourceInfo>() {
			@Override
			public Predicate toPredicate(Root<ResourceInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new LinkedList<>();
				if (!Tools.empty(resourceInfo.getResourceName())) {
					predicates.add(cb.like(root.get("resourceName"), //
						"%".concat(resourceInfo.getResourceName()).concat("%")));
				}
				if (!Tools.empty(resourceInfo.getResourceType())) {
					predicates.add(cb.equal(root.get("resourceType"), //
						resourceInfo.getResourceType()));
				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};
	}
	
	// predicates.add(cb.equal(cb.substring(root.get("datagram"), 1, 23), dataPrefix));

}
